using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

using System.Reflection;
using System.Text;
using System.Windows.Forms;

using Mud.Common;
using Mud.Common.Communication;
using Mud.Common.Messages;
using System.Drawing;

namespace Mud.Client.WinApplication
{
    public partial class frmMain : Form, IClientShell
    {

        private Client _client;
        private AreaView _areaView;

        public frmMain()
        {
            InitializeComponent();
            _client = new Client(this);
            _client.ClientConnected += ClientConnected;
            _client.ClientDisconnected += ClientDisconnected;
            _client.ClientNewPlayerCharacter += ClientNewCharacter;
            Rectangle bounds = Screen.PrimaryScreen.WorkingArea;
            this.Bounds = bounds;
            Logger.AddSink(this.logTextBox1);
			Logger.AddSink(new FileLogSink());
        }

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            _areaView = new AreaView(_client);
            timerMain.Enabled = true;
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (_client.Connected)
                _client.Disconnect();
			Logger.ClearSinks();
            base.OnFormClosing(e);
        }

        private void tsbConnectDisconnect_Click(object sender, EventArgs e)
        {
            if (_client.Connected)
            {
                if (!_client.Logout())
                    _client.Disconnect();
            }
            else
            {
                using (frmConnectionSettings settings = new frmConnectionSettings())
                {
                    if (settings.ShowDialog() != DialogResult.OK)
                        return;
                    if (_client.Connect(settings.IPAddress, settings.IPPort, settings.Username, settings.Password))
                        _client.StartPlaying();

                }
            }
        }


        #region Client related methods

        Creature IClientShell.SelectCreature(IList<Creature> creatures)
        {
            using (frmChooseCharacter form = new frmChooseCharacter(creatures))
            {
                if (form.ShowDialog() != DialogResult.OK)
                    return null;
                return form.Selected;
            }
        }


        void ClientConnected()
        {
            this.tsbConnectDisconnect.Text = "Disconnect";
            RefreshCaption();
        }

        void ClientDisconnected()
        {
            this.tsbConnectDisconnect.Text = "Connect";
            RefreshCaption();
        }

        void ClientNewCharacter(Creature oldCharacter, Creature newCharacter)
        {
            RefreshCaption();
        }

        #endregion

        /// <summary>
        /// Drives the client.
        /// </summary>
        private void timerMain_Tick(object sender, EventArgs e)
        {
            if(_client.Connected)
                _client.Tick();
            _areaView.Update();
        }

        /// <summary>
        /// Refresh the window caption.
        /// </summary>
        private void RefreshCaption()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("MudClient v ");
            builder.Append(Assembly.GetExecutingAssembly().GetName().Version.ToString());
            builder.Append(" ");
            if (!_client.Connected)
            {
                builder.Append("- not connected -");
            }
            else
            {
                builder.Append("Connected to '");
                builder.Append(_client.ServerName);
                builder.Append("' as '");
                builder.Append(_client.UserName);
                builder.Append("'");
            }
            this.Text = builder.ToString();
        }

    }
}